package com.seecen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seecen.pojo.entity.UserCoupon;
import com.seecen.service.UserCouponService;
import com.seecen.mapper.UserCouponMapper;
import org.springframework.stereotype.Service;

/**
* @author 小辛
* @description 针对表【allmaster__user_coupon(用户优惠券关联表)】的数据库操作Service实现
* @createDate 2025-10-19 14:32:47
*/
@Service
public class UserCouponServiceImpl extends ServiceImpl<UserCouponMapper, UserCoupon>
    implements UserCouponService{

    @Override
    public boolean hasUserClaimedCoupon(Integer userId, Integer couponId) {
        QueryWrapper<UserCoupon> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                .eq("coupon_id", couponId);
        return this.count(queryWrapper) > 0;
    }

    @Override
    public String generateCouponCode() {
        // 生成优惠券码：CPN + 时间戳后6位 + 随机4位数字
        String timestamp = String.valueOf(System.currentTimeMillis());
        String timestampSuffix = timestamp.substring(timestamp.length() - 6);
        String randomSuffix = String.format("%04d", (int)(Math.random() * 10000));
        return "CPN" + timestampSuffix + randomSuffix;
    }
}




